package com.itcast.web;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.itcast.interfaces.api.CommandOperatorAccountService;
import com.itcast.utils.BizResult;
import com.itcast.vo.OperatorAccountVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RefreshScope
@Api(tags = {"管理员账户相关接口"})
public class MeetController {

    @Value("${shareInfo:''}")
    private String shareInfo;

    @Reference(version = "1.0.0",check = false)
    private CommandOperatorAccountService commandOperatorAccountService;

    /**
     * 处理获取会议信息的请求。
     *
     * @return 返回一个字符串，内容为"hello meet"，表示欢迎来到会议页面。
     */
    @RequestMapping(value = "/getMeet",method = RequestMethod.GET)
    public String getMeet(){
        initFlowQpsRule();
        try(Entry entry = SphU.entry("getMeet")){
            return "hello meet";
        }catch (BlockException e){
            e.printStackTrace();
            return "hello meet catch  已被限流";
        }finally {

        }
    }

    private static void initFlowQpsRule() {
        List<FlowRule> rules = new ArrayList<FlowRule>();
        FlowRule rule1 = new FlowRule();
        rule1.setResource("getMeet");
        // set limit qps to 20
        rule1.setCount(2);
        rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule1.setLimitApp("default");
        rules.add(rule1);
        FlowRuleManager.loadRules(rules);
    }


    @GetMapping("/getConfigInfo")
    @SentinelResource(value = "getConfigInfo",blockHandler = "getConfigInfoBlockHandler")
    public String getConfigInfo(){
        return "shareInfo:  "+this.shareInfo;
    }

    public String getConfigInfoBlockHandler(BlockException e){
        return "shareInfo:  "+this.shareInfo+"  "+e.getMessage();
    }

    @GetMapping("/login")
    @ApiOperation("登录")
    public BizResult login() {

        return commandOperatorAccountService.login("","");
    }

}
